package com.zthz.douxing

import com.zthz.douxing.utils.ValidateConstanst

class Person {

    transient springSecurityService

    String id
    String username
    String password

    String realName             //真实姓名
    IdType idType               //证件类型
    String idNo                 //证件号码
    Date birthday = null        //生日，根据这个可以判断是儿童还是成人
    String mobileNo             //手机号码绑定
    String email                //邮箱
    String thirdUserId          //三方用户ID
    String thirdUserName        //三方用户名
    ThirdSystemType thirdType   //绑定用户的类型
    UserLevel userLevel         //表示用户级别
    String accessToken          //三方用户的授权码，这个将会长期有效。
    String iconUrl              //用户头像地址
    Date lastLoginTime          //最近登陆时间
    String commonTravelWay      //常用旅行方式
    String xgToken              //信鸽Token

    String getuiId              //getui的clientId

    boolean enabled = true
    boolean accountExpired
    boolean accountLocked
    boolean passwordExpired


    static transients = ['springSecurityService']

    static hasMany = [frequentPassengers: FrequentPassenger, frequentContacts: FrequentContact,
            plantrips: Plantrip, thirdAccount: ThirdAccountBinding, douXingLog: DouXingLog,oAuthIDs: OAuthID]

    static constraints = {
        username blank: false, unique: true
        password blank: false
        birthday nullable: true
        idNo nullable: true
        idType nullable: true
        realName nullable: true
        thirdType nullable: true
        userLevel nullable: true
        commonTravelWay nullable: true

        email nullable: true,email: true
        mobileNo matches: ValidateConstanst.MobileNoPattern, nullable: true
        thirdUserId nullable: true
        thirdUserName nullable: true
        accessToken nullable: true
        lastLoginTime nullable: true
        iconUrl nullable: true //如果没有就使用默认图

        xgToken nullable: true,blank: true
        getuiId nullable: true,blank: true
        oAuthIDs nullable: true,blank:true
    }

    def beforeValidate() {
        log.info(birthday)
    }

    static mapping = {
        id generator: 'uuid'
        password column: '`password`'
        frequentPassengers lazy: true
        frequentContacts lazy: true
        plantrips lazy: true
    }

    Set<Authority> getAuthorities() {
        PersonAuthority.findAllByPerson(this).collect { it.authority } as Set
    }

    def beforeInsert() {
        encodePassword()
    }

    def beforeUpdate() {
        if (isDirty('password')) {
            encodePassword()
        }
    }

    protected void encodePassword() {
        password = springSecurityService.encodePassword(password)
    }


    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("Person{");
        sb.append("id='").append(id).append('\'');
        sb.append(", username='").append(username).append('\'');
        sb.append(", password='").append(password).append('\'');
        sb.append(", realName='").append(realName).append('\'');
        sb.append(", idType=").append(idType);
        sb.append(", idNo='").append(idNo).append('\'');
        sb.append(", birthday=").append(birthday);
        sb.append(", mobileNo='").append(mobileNo).append('\'');
        sb.append(", email='").append(email).append('\'');
        sb.append(", thirdUserId='").append(thirdUserId).append('\'');
        sb.append(", thirdUserName='").append(thirdUserName).append('\'');
        sb.append(", thirdType=").append(thirdType);
        sb.append(", userLevel=").append(userLevel);
        sb.append(", accessToken='").append(accessToken).append('\'');
        sb.append(", enabled=").append(enabled);
        sb.append(", accountExpired=").append(accountExpired);
        sb.append(", accountLocked=").append(accountLocked);
        sb.append(", passwordExpired=").append(passwordExpired);
        sb.append('}');
        return sb.toString();
    }
}

enum ThirdSystemType {
    None, //表示本系统用户
    QQ,
    WeiBo,
    Google;
}

/**
 * 证件类型
 */
enum IdType {
    ID,
    PASSPORT,
    OTHER;
}

enum UserLevel {
    NORMAL,
    VIP;
}
